package com.socool.sinofaith.ui;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.adapter.SyncSubTaskAdapter;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.SynTaskResponse;
import com.socool.sinofaith.util.BGSharedPreference;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.ProgressDialog;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import net.tsz.afinal.utils.Utils;
/**
 * 提交统计
 * @author jiang
 *
 */
public class SyncTaskActivity extends BaseActivity
		implements CenterViewFactory, SpinnerViewFactory, View.OnClickListener {

	public static final String TASK_ID = SyncTaskActivity.class.getName() + "TASK_ID";
	public static final String TASK_NAME = SyncTaskActivity.class.getName() + "TASK_NAME";
	private String task_id;
	private String task_name;

	ActionBarHelperBase mActionBarHelperBase;

	private ListView lv;
	SyncSubTaskAdapter adapter;

	Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sync_task);

		SocoolApplication.addActivity(this);

		if (getIntent() != null && getIntent().hasExtra(TASK_ID)) {
			task_id = getIntent().getStringExtra(TASK_ID);
			Log.i("jsc", "task_id:"+task_id);
		} else {
			finish();
		}

		if (getIntent() != null && getIntent().hasExtra(TASK_NAME)) {
			task_name = getIntent().getStringExtra(TASK_NAME);
		}

		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);

		lv = (ListView) findViewById(R.id.lv);
		adapter = new SyncSubTaskAdapter(this);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(SyncTaskActivity.this, SyncWorkActivity.class);
				intent.putExtra(SyncWorkActivity.SYN_SUBTASK, adapter.getItem(position));
				startActivity(intent);
			}
		});

		dialog = new ProgressDialog(this);
		getData();
	}

	@Override
	public void onClick(View v) {
	}

	@Override
	public View createSpinnerView() {
		return new View(this);
	}

	@Override
	public View createView() {
		TextView tv = new TextView(this);
		tv.setText(task_name);
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		return tv;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.common, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	void getData() {
		FinalHttp fh = new FinalHttp();
		final AjaxParams params = new AjaxParams();
		params.put("dataType", "0");
		params.put("sid", ConstansJsonUrl.syncTask);
		params.put("did", SinofaithUtil.did);
		params.put("taskid", task_id);
		fh.post(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack<String>() {
			@Override
			public void onStart() {
				dialog.show();
			}

			@Override
			public void onSuccess(String body) {
				dialog.cancel();
				BGSharedPreference.put(SyncTaskActivity.this, Utils.statistical+task_id, body);
				SynTaskResponse response = null;
				try {
					response = HttpJsonAdapter.getInstance().get(body, SynTaskResponse.class);
				} catch (Exception e) {
				}
				if (response != null && response.getResult() == 0) {
					adapter.replace(response.getData().getSubtasks());
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				dialog.cancel();
			}
		});
	}
}
